package com.xrkj.b2c.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sun.corba.se.spi.orb.StringPair;
import com.xrkj.b2c.controller.common.BasicController;
import com.xrkj.b2c.model.sys.SysMenu;
import com.xrkj.b2c.service.sys.SysMenuService;
import com.xrkj.b2c.utils.StringUtil;
import com.xrkj.b2c.utils.UUIDUtil;

/**
 * 
 * <B>系统名称：</B><BR>
 * <B>模块名称：</B><BR>
 * <B>中文类名：</B>菜单控制器<BR>
 * <B>概要说明：</B><BR>
 * @author （赵子靖）
 * @since 2018年07月22日
 */
@RequestMapping("/sysMenu")
@Controller
public class SysMenuController extends BasicController {

    @Autowired
    private SysMenuService menuService;

    private Logger logger = Logger.getLogger(SysMenuController.class);

    /**
     * 
     * <B>方法名称：</B>转向管理页面<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author Tom
     * @since 2017年10月9日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardMenu")
    public ModelAndView forwardMenu(HttpServletRequest request, HttpServletResponse response) {
        String is = request.getParameter("t");
        request.setAttribute("is", is);
        return new ModelAndView("WEB-INF/pages/sys/sys-menu/menu-to-manager");
    }

    /**
     * 
     * <B>方法名称：</B>获取列表<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年10月20日
     * @param request
     * @param response
     * @param pageSize
     * @param pageNo
     * @return
     */
    @RequestMapping("/menuList")
    @ResponseBody
    public Object menuList(HttpServletRequest request, HttpServletResponse response) {
        String search = request.getParameter("search");//名称
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("offset", StringUtil.isEmpty(offset) ? null : Integer.parseInt(offset));
        paramter.put("limit", StringUtil.isEmpty(limit) ? null : Integer.parseInt(limit));
        paramter.put("search", search);
        paramter.put("mParentId", "root");
        Integer total = menuService.findMenuTotal(paramter);
        List<SysMenu> menus = new ArrayList<SysMenu>();
        if (total > 0) {
            menus = menuService.findMenuList(paramter);
        }
        ret.put("total", total);
        ret.put("rows", menus);
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>转向新增方法<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年10月20日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardAddMenu")
    public ModelAndView forwardAddMenu(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("WEB-INF/pages/sys/sys-menu/add-menu-dialog");
    }

    /**
     * 
     * <B>方法名称：</B>新增<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月6日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/addMenu")
    @ResponseBody
    public Map<String, Object> addMenu(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String mParentId = request.getParameter("mParentId");
        String mName = request.getParameter("mName");
        String mUrl = request.getParameter("mUrl");
        String mStatus = request.getParameter("mStatus");
        String mSort = request.getParameter("mSort");
        String mIcon = request.getParameter("mIcon");
        String mPrivilegeCode = request.getParameter("mPrivilegeCode");

        if (StringUtil.isEmpty(mName)) {
            ret.put("success", false);
            ret.put("msg", "保存失败，请输入菜单名称");
            return ret;
        }
        SysMenu menu = new SysMenu();
        menu.setmId(UUIDUtil.id());
        menu.setmParentId(mParentId);
        menu.setmName(mName);
        menu.setmUrl(mUrl);
        menu.setmIcon(mIcon);
        menu.setmPrivilegeCode(mPrivilegeCode);
        menu.setmSort(StringUtil.isEmpty(mSort)?0:Integer.parseInt(mSort));
        menu.setmStatus(StringUtil.isEmpty(mStatus) ? 0 : Short.parseShort(mStatus));
        if ((menuService.insertMenu(menu) > 0)) {
            ret.put("success", true);
            ret.put("msg", "保存成功。");
        } else {
            ret.put("success", false);
            ret.put("msg", "保存失败。");
        }
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>跳转修改页面<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月6日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardUpdateMenu")
    public ModelAndView forwardUpdateMenu(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");//编号
        SysMenu menu = menuService.selectByPrimaryKey(id);
        if(menu!=null){
            menu.setParentMenu(menuService.selectByPrimaryKey(menu.getmParentId()));
        }
        request.setAttribute("item", menu);
        return new ModelAndView("WEB-INF/pages/sys/sys-menu/update-menu-dialog");
    }

    /**
     * 
     * <B>方法名称：</B>根据ID获取信息<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月8日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getMenuById")
    @ResponseBody
    public Map<String, Object> getMenuById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");//编号
        SysMenu menu = menuService.selectByPrimaryKey(id);
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("item", menu);
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>新增<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月6日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/updateMenu")
    @ResponseBody
    public Map<String, Object> updateMenu(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String mId = request.getParameter("mId");
        String mParentId = request.getParameter("mParentId");
        String mName = request.getParameter("mName");
        String mUrl = request.getParameter("mUrl");
        String mStatus = request.getParameter("mStatus");
        String mSort = request.getParameter("mSort");
        String mIcon = request.getParameter("mIcon");
        String mPrivilegeCode = request.getParameter("mPrivilegeCode");

        if (StringUtil.isEmpty(mName)) {
            ret.put("success", false);
            ret.put("msg", "保存失败，请输入菜单名称");
            return ret;
        }
        
        SysMenu menu = menuService.selectByPrimaryKey(mId);
        if (menu == null) {
            ret.put("success", false);
            ret.put("msg", "保存失败，未获取到修改对象。");
            return ret;
        }
        menu.setmParentId(mParentId);
        menu.setmName(mName);
        menu.setmUrl(mUrl);
        menu.setmIcon(mIcon);
        menu.setmPrivilegeCode(mPrivilegeCode);
        menu.setmSort(StringUtil.isEmpty(mSort)?0:Integer.parseInt(mSort));
        menu.setmStatus(StringUtil.isEmpty(mStatus) ? 0 : Short.parseShort(mStatus));
        
        if ((menuService.updateMenu(menu)) > 0) {
            ret.put("success", true);
            ret.put("msg", "保存成功。");
        } else {
            ret.put("success", false);
            ret.put("msg", "保存失败。");
        }
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>删除<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author Tom
     * @since 2017年10月11日
     * @return
     */
    @RequestMapping("/deleteMenu")
    @ResponseBody
    public Map<String, Object> deleteMenu(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String id = request.getParameter("id");
        if (menuService.deleteMenu(id) > 0) {
            //删除成功
            ret.put("success", true);
            ret.put("msg", "删除成功。");
        } else {
            //删除失败
            ret.put("success", false);
            ret.put("msg", "删除失败");
        }
        return ret;
    }
    
    /**
     * 
     * <B>方法名称：</B>获取子节点<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年11月29日 	
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getMenuByParent")
    @ResponseBody
    public Map<String, Object> getMenuByParent(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String mParentId = request.getParameter("mParentId");
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("mParentId", mParentId);
        List<SysMenu> menus = menuService.findMenuList(paramter);
        ret.put("rows", menus);
        return ret;
    }
    
    /**
     * 
     * <B>方法名称：</B>获取菜单列表<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年12月4日 	
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getMenus")
    public ModelAndView getMenus(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("mParentId", "root");
        paramter.put("mStatus", SysMenu.STATUS_1);
        List<SysMenu> menus = menuService.findMenuList(paramter);
        if(menus!=null && menus.size()>0){
            for (SysMenu menu : menus) {
                Map<String, Object> subParamter = new HashMap<String, Object>();
                subParamter.put("mParentId", menu.getmId());
                subParamter.put("mStatus", SysMenu.STATUS_1);
                menu.setSubSysMenus(menuService.findMenuList(subParamter));
            }
        }
        request.setAttribute("rows", menus);
        return new ModelAndView("WEB-INF/pages/common/menu");
    }
    
    
}
